$(function () {
    $('.add').click(function (e) {
        $('#name').val('');
        $('#password').val('');


    });
    $('#save').click(function (e) {
        e.preventDefault();
        var uid = $(this).attr('uid');
        var type = $(this).attr('type');
        var name = $('#name').val();
        var password = $('#password').val();
        var url = '';
        if (type == 1) {
            url = '/admin/modbuser'
        } else {
            url = '/admin/addbuser'
        }
        zlajax.post({
            'url': url,
            'data': {
                'name': name,
                'uid': uid,
                'password': password,
            },
            'success': function (data) {
                if (data['code'] == 200) {
                    xtalert.alertSuccessToast('操作成功！');
                    setTimeout(function () {
                        window.location.reload()
                    }, 700);
                } else {
                    xtalert.alertInfo(data['msg'])
                }
            },
            'fail': function () {
                xtalert.alertNetworkError()
            }
        })
    });

    $('.mod-btn').click(function (e) {
        e.preventDefault();
        var uid = $(this).parent().attr('uid');
        var info = $(this).parent().siblings();
        var name = info.eq(1).html();

        $('#save').attr({
            "type": 1,
            "uid": uid,
        });

        $('#name').val(name);

    });
    $('.del-btn').click(function (e) {
        e.preventDefault();
        var uid = $(this).parent().attr('uid');
        xtalert.alertConfirm({
            'title': '确认删除吗',
            'confirmCallback': function () {
                zlajax.get({
                    'url': '/admin/delbuser',
                    'data': {
                        'uid': uid
                    },
                    'success': function (data) {
                        if (data['code'] == 200) {
                            xtalert.alertSuccessToast('删除成功！');
                            setTimeout(function () {
                                window.location.reload()
                            }, 700)
                        } else {
                            xtalert.alertInfo(data['msg'])
                        }
                    },
                    'fail': function () {
                        xtalert.alertNetworkError()
                    }
                })
            }
        })
    });
});